
#include <BStream>
#include "member_BYamlDocument.h"

using namespace BWE;

member_BYamlDocument::member_BYamlDocument(BYamlDocument* sondocument)
{
	boss = sondocument;
	levelIndent = 1;
}
member_BYamlDocument::~member_BYamlDocument()
{

}

int member_BYamlDocument::readLevel(const BString& text)
{
	int pos = 0;
	while (pos < text.size())
	{
		char c = text[pos];
		if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v')
			pos++;
		else
			break;
	}
	return pos;
}
void member_BYamlDocument::arrangeIndent(BYamlNode* node)
{
	member_BYamlNode* yamlmem = yaml_member(node);
	for (int i = 0; i < node->childCount(); i++)
	{
		BYamlNode* child = node->child(i);
		arrangeIndent(child);
	}
	if (yamlmem->parent)
	{
		yamlmem->indent -= yamlmem->parent->indent();
	}
}

void member_BYamlDocument::writeNode(const BYamlNode* node, BString& yamlText, int indent)
{
	member_BYamlNode* yamlmem = yaml_member(node);
	yamlmem->indent = indent;
	yamlText.append(' ', indent);
	yamlText << node->name() << ": " << yamlmem->text;
	if (yamlmem->comment.size())
		yamlText << " #" << yamlmem->comment;
	yamlText << '\n';
	if (yamlmem->children.size())
	{
		int lowerIndent = node->child(0)->indent();
		if (lowerIndent <= 0)
			lowerIndent = levelIndent;
		if (lowerIndent < 1)
			lowerIndent = 1;
		indent += lowerIndent;
		for (int i = 0; i < node->childCount(); i++)
		{
			const BYamlNode* child = node->child(i);
			writeNode(child, yamlText, indent);
		}
	}
}
void member_BYamlDocument::writeNode(const BYamlNode* node, BStream& stream, int indent)
{
	member_BYamlNode* yamlmem = yaml_member(node);
	yamlmem->indent = indent;
	stream.write(' ', indent);
	stream << node->name() << ": " << yamlmem->text;
	if (yamlmem->comment.size())
		stream << " #" << yamlmem->comment;
	stream << '\n';
	if (yamlmem->children.size())
	{
		int lowerIndent = node->child(0)->indent();
		if (lowerIndent <= 0)
			lowerIndent = levelIndent;
		if (lowerIndent < 1)
			lowerIndent = 1;
		indent += lowerIndent;
		for (int i = 0; i < node->childCount(); i++)
		{
			const BYamlNode* child = node->child(i);
			writeNode(child, stream, indent);
		}
	}
}

